<?php

namespace app\controller\admin;

use app\BaseController;
use think\facade\Db;

class RoleController extends BaseController
{

     public function getList()
     {
         $page = input('pageNo');
         $limit = input('pageSize');
         $role = input('role');

         $where = ' 1=1 ';
         if ($role) {
             $where = " role_name  like '%$role%' ";
         }
         $list = Db::table("role")
             ->where($where)
             ->limit($limit * ($page - 1), $limit)
             ->select()
             ->each(function($data){
                 $data['role'] = $data['role_name'];
                 $data['treeList'] = json_decode($data['treeList'], true);
                 $data['btnRolesCheckedList'] = json_decode($data['btnChecked'], true);
                 return $data;
             });
         $total = Db::table("role")->where($where)->count();
         $returnD['list'] = $list;
         $returnD['total'] = $total;
         return retJson($returnD);
     }

    public function doEdit()
    {
        $data = input("");
        $id = isset($data['id'])?$data['id']:'';
        $role_name  = $data['role'];
        $btnRolesCheckedList = $data["btnRolesCheckedList"];
        $treeList = $data['treeArray:'];

        $calcData['role_name'] = $role_name;
        $calcData['btnChecked'] = json_encode($btnRolesCheckedList);
        $calcData['treeList'] = json_encode($treeList);
        if($id){
            Db::table("role")->where("id",$id)->update($calcData);
        }else{
            Db::table("role")->insert($calcData);
        }

       return  retJson();
    }

    public function doDelete()
    {
        $data = input("");
        $ids=  $data['ids'];

        Db::table("role")->where("id",$ids)->delete();

        return retJson();
    }
}
